package com.tiancheng.trade.payment.controller.api;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.payment.channel.bo.GetUnifiedOrderResBO;
import com.tiancheng.trade.payment.service.QrFlowService;
import com.tiancheng.trade.payment.vo.api.*;
import com.tiancheng.trade.commom.web.model.response.UnifiedOrderResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import jakarta.validation.Valid;

/**
 * @Author: likailun
 * @Description: 二维码消费
 * @Date: create in 2021/8/28 9:45 上午
 */
@RestController
@RequestMapping("/api/qrc")
@Validated
@Slf4j
public class QrFlowController {

    @Resource
    private QrFlowService qrFlowService;
    /**
     * 二维码被扫统一下单
     * @param qrcUnifiedOrderVo
     * @return
     */
    @PostMapping("/unified")
    public Result<UnifiedOrderResVO> unified(@RequestBody @Valid QrcUnifiedOrderVo qrcUnifiedOrderVo){
        log.info("二维码支付下单数据:{}", JsonUtil.toJsonString(qrcUnifiedOrderVo));
        return Result.success(qrFlowService.unifiedOrder(qrcUnifiedOrderVo));
    }

    /**
     * 二维码被扫统一下单，查询支付结果
     * @param getUnifiedOrderVo
     * @return
     */
    @PostMapping("/queryPayResult")
    public Result<GetUnifiedOrderResBO> query(@RequestBody @Valid GetUnifiedOrderVo getUnifiedOrderVo){
        return qrFlowService.queryPayResult(getUnifiedOrderVo);
    }

    /**
     * 关闭二维码订单
     * @param closeOrderVO
     * @return
     */
    @PostMapping("/close")
    public Result closeOrder(@RequestBody @Valid CloseOrderVO closeOrderVO) {
        log.info("关闭订单:{}", JsonUtil.toJsonString(closeOrderVO));
        return qrFlowService.closeOrder(closeOrderVO);
    }

    /**
     * 关闭二维码订单
     * @param closeOrderVO
     * @return
     */
    @PostMapping("/closeByOrderId")
    public Result closeByOrderId(@RequestBody @Valid CloseQrOrderVO closeOrderVO) {
        log.info("关闭二维码订单:{}", JsonUtil.toJsonString(closeOrderVO));
        return qrFlowService.closeByOrderId(closeOrderVO);
    }
}
